<?php

namespace app\common\controller;

use think\Controller;
use external\SFTools;

class Oauth extends Controller {
    protected $userFind;

    protected function initialize() {
        $this->pvt_loginCheck();
    }

    protected function toHomePage() {
        $action = request()->action();
        // 设备 [ pc / mob / ipad ]
        $device = SFTools::getDevice();
        // 窗口 [ wechat / dingding / browser]
        $window = SFTools::getWindow();
        switch ($window) {
            case 'wechat'://微信环境 处理
                if ($window == $action) {
                    $this->getUserFind();
                }else{
                    $url = "/home/Login/index.html";
                }
                break;
            case 'dingding'://钉钉环境 处理
                if ($window != $action) {
                    $url = "/home/Login/dingding.html";
                }
                break;
            default: //浏览器环境 处理
                if ($action != 'index') {
                    // 平板 和 PC
                    $url = "/home/Login/index.html";
                }
                /*if ($device === 'mob' && $device != $action) {
                    //手机
                    $url = "/home/Login/mob.html";
                }
                if (($device === 'pc' || $device === 'ipad') && $action != 'index') {
                    // 平板 和 PC
                    $url = "/home/Login/index.html";
                }*/
                break;
        }
        if (!empty($url)) {
            $this->redirect($url);
            exit;
        }
        $this->pvt_setPageConstant();
    }
    
    protected function toLoginHomePage() {
        $device = SFTools::getDevice();
        // 窗口 [ wechat / dingding / browser]
        $window = SFTools::getWindow();
        switch ($window) {
            case 'wechat'://微信环境 处理
                $redirect_url = "/system/Order/search.html";
                break;
            case 'dingding'://钉钉环境 处理
                $redirect_url = "/system/Order/search.html";
                break;
            default: //浏览器环境 处理
                if ($device == 'mob') {
                    //手机浏览器首页地址
                    $redirect_url = "/system/Order/search.html";
                } else {
                    // PC & iPad 浏览器首页地址
                    $redirect_url = "/pc/Order/search.html";
                }
                break;
        }
        if (!empty($redirect_url)) {
            $this->redirect($redirect_url);
            exit;
        }
    }

    protected function setTicket($userFindNew) {
        if (!empty($userFindNew)) {
            // 窗口 [ wechat / dingding / browser]
            $window = SFTools::getWindow();
            switch ($window) {
                case 'wechat'://微信环境 处理
                    $cache_key_str =  $userFindNew['openid'];
                    break;
                case 'dingding'://钉钉环境 处理
                    $cache_key_str =  "";
                    break;
                default: //浏览器环境 处理
                    $cache_key_str = SERVICE_APP_ID . $userFindNew['user_uuid'];
                    break;
            }
            $cache_key = md5($cache_key_str);
            $userFind = SFTools::getCache($cache_key);
            if (empty($userFind)) {
                $userFind = array();
            }
            foreach ($userFindNew as $key => $value) {
                $userFind[$key] = $value;
            }
            $this->userFind = $userFind;
            SFTools::setCache($cache_key, $userFind);
        }
    }

    protected function getUserFind() {
        // 窗口 [ wechat / dingding / browser]
        $window = SFTools::getWindow();
        $userFind = $this->pvt_getCacheUserFind();
        if (empty($userFind)) {
            switch ($window) {
                /*case 'wechat'://微信环境 处理
                    $wechatUserLogic = new \backend\logic\WechatUserLogic;
                    $userFind = $wechatUserLogic->getWechatUserFindToTicket();
                    $user_uuid = $userFind['user_uuid'];
                    if(!empty($user_uuid)) {
                        $cstMstLogic = new \backend\logic\CstMstLogic;
                        $cstMstFind = $cstMstLogic->getCstMstFindBuyUserUUID($user_uuid);
                        if(!empty($cstMstFind)) {
                            $userFind['fccode'] = $cstMstFind['fccode'];
                            $userFind['mkCode'] = $cstMstFind['mkCode'];
                        }
                    }
                    break;*/
                case 'dingding'://钉钉环境 处理
                    $userFind = array();
                    break;
                default: //浏览器环境 处理
                    $userFind = array();
                    break;
            }
            $this->setTicket($userFind);
        }
        return $userFind;
    }

    protected function user_logout() {
        $cookie_key = SFTools::getCookieKey();
        $cache_key = cookie($cookie_key);
        if(!empty($cache_key)){
            // 窗口 [ wechat / dingding / browser]
            /*$window = SFTools::getWindow();
            switch ($window) {
                case 'wechat'://微信环境 处理
                    $userFind = SFTools::getCache($cache_key);
                    $openid = $userFind['openid']? $userFind['openid'] : $this->userFind['openid'];
                    $wechatUserLogic = new \backend\logic\WechatUserLogic;
                    $wechatUserLogic->saveWechatUserFindBuyUserUUID($openid, '');
                    break;
                case 'dingding'://钉钉环境 处理
                    
                    break;
                default:
                    break;
            }*/
            SFTools::setCache($cache_key, NULL);
        }
        if(!empty($cookie_key)) {
            setcookie($cookie_key, NULL, 0, '/');
        }
        $this->pvt_toLoginPage();
    }
    
    protected function getJsSignPackage() {
        // 窗口 [ wechat / dingding / browser]
        $window = SFTools::getWindow();
        if($window == 'wechat') {
            $config = config('wechat_config_xhb');
            $wechatAPI = new \external\WechatAPI($config['appid'], $config['appsecret']);
            $signPackage = $wechatAPI->getJsSignPackage();
            $this->assign('signPackage', $signPackage);
        }
    }

    private function pvt_toLoginPage() {
        // 设备 [ pc / mob / ipad ]
        $device = SFTools::getDevice();
        // 窗口 [ wechat / dingding / browser]
        $window = SFTools::getWindow();
        // 平板 和 PC (默认)
        $url = "/home/Login/index.html";
        switch ($window) {
            case 'wechat'://微信环境 处理
                $url = "/home/Login/index.html";
                break;
            case 'dingding'://钉钉环境 处理
                $url = "/home/Login/dingding.html";
                break;
            default: //浏览器环境 处理
                $url = "/home/Login/index.html";
                /*if ($device === 'mob') {
                    //手机
                    $url = "/home/Login/mob.html";
                }*/
                break;
        }
        $this->redirect($url);
        exit;
    }

    private function pvt_loginCheck() {
        $userFind = $this->getUserFind();
        $module = strtolower(request()->module());
        $controller = strtolower(request()->controller());
        $is_login = false;
        // 窗口 [ wechat / dingding / browser]
        $window = SFTools::getWindow();
        if ($module == 'home' && $controller == 'register') {
            if ($window != 'browser' && empty($userFind)) {
                $is_login = true;
            }
        }
        else {
            /*if ($window == 'wechat') {
                if (empty($userFind['user_uuid']) || empty($userFind['user_auth'])){
                    $is_login = true;
                }
            }
            else {*/
                if (empty($userFind)) {
                    $is_login = true;
                }
           // }
        }
        if ($is_login) {
            $this->pvt_toLoginPage();
        }
        $this->pvt_setPageConstant();
    }

    private function pvt_getCacheUserFind() {
        $cookie_key = SFTools::getCookieKey();
        $cache_key = cookie($cookie_key);
        $userFind = array();
        if (!empty($cache_key)) {
            $userFind = SFTools::getCache($cache_key);
        }
        if(!empty($userFind)){
            $this->userFind = $userFind;
        }
        return $userFind;
    }

    private function pvt_setPageConstant() {
        // 设备 [ pc / mob / ipad ]
        $device = SFTools::getDevice();
        // 窗口 [ wechat / dingding / browser]
        $window = SFTools::getWindow();
        $module = strtolower(request()->module());
        $controller = strtolower(request()->controller());
        $action = strtolower(request()->action());
        $direction = 'transverse';
        /*if ($device == 'mob') {
            $direction = 'vertical';
        }*/
        $style_bg = '#ec708a';
        if(isset($this->userFind['mkCode'])){
            switch ($this->userFind['mkCode']){
                case "26":
                    $style_bg = '#0364BD';
                    break;
            }
        }
        $page_static_public = PAGE_STATIC_ROOT . "/{$direction}/";
        $page_static_folder = PAGE_STATIC_ROOT . "/{$direction}/{$module}/{$controller}/{$action}/";
        $this->assign('PAGE_STATIC_PUBLIC', $page_static_public);
        $this->assign('PAGE_STATIC_FOLDER', $page_static_folder);
        $this->assign('MODEL', $module);
        $this->assign('CONTROLLER', $controller);
        $this->assign('ACTION', $action);
        $this->assign('STYLE_BG', $style_bg);
        $this->assign('device', $device);
        $this->assign('window', $window);
    }
}
